package com.ruoyi.system.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.ApplyToolInfo;
import com.ruoyi.system.api.domain.ToolInfo;
import com.ruoyi.system.api.service.PokeRequestServe;
import com.ruoyi.system.api.service.ToolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SparePartsServiceImpl
 * @Description: TODO
 * @Author wlf
 * @Date 2020/3/2
 * @Version V1.0
 **/
@Service
public class ToolServiceImpl implements ToolService {

    private static final Logger log = LoggerFactory.getLogger(ToolServiceImpl.class);

    @Autowired
    private PokeRequestServe pokeRequestServe;

    /**
     * 配置文件
     */
    @Autowired
    private Global global;

    @Override
    public ResponseWithData<?> getToolByFse(Map<String, String> params) {
        String ccServerUrl = global.getCcServerUrl();
        StringBuffer url = new StringBuffer();
        url.append(ccServerUrl);
        url.append("getToolByFse.asmx/getToolByFseHandle");
        StringBuffer postParam = new StringBuffer();
        postParam.append("enginner_id=").append(params.get("enginnerId"));
        postParam.append("&Status=").append(StringUtils.isEmpty(params.get("status")) ? "" : params.get("status"));
        postParam.append("&tool_no=").append(StringUtils.isEmpty(params.get("toolNo")) ? "" : params.get("toolNo"));
        postParam.append("&Tool_name=").append(StringUtils.isEmpty(params.get("toolNname")) ? "" : params.get("toolNname"));

        return pokeRequestServe.transPost(url.toString(), "获取工程师名下工具接口", "getToolByFse", postParam.toString());
    }

    @Override
    public ResponseWithData<?> getBorrowTool(Map<String, String> params) {
        String ccServerUrl = global.getCcServerUrl();
        StringBuffer url = new StringBuffer();
        url.append(ccServerUrl);
        url.append("getBorrowTool.asmx/getBorrowToolHandle");

        StringBuffer postParam = new StringBuffer();
        postParam.append("enginner_id=").append(params.get("enginnerId"));
        postParam.append("&Status=").append(StringUtils.isEmpty(params.get("status")) ? "" : params.get("status"));
        postParam.append("&tool_no=").append(StringUtils.isEmpty(params.get("toolNo")) ? "" : params.get("toolNo"));
        postParam.append("&Tool_name=").append(StringUtils.isEmpty(params.get("toolNname")) ? "" : params.get("toolNname"));

        return pokeRequestServe.transPost(url.toString(), "获取工具库可借用的工具接口", "getBorrowTool", postParam.toString());
    }

    @Override
    public ResponseWithData<?> toolReceiving(Map<String, Object> params) {
        String ccServerUrl = global.getCcServerUrl();
        StringBuffer url = new StringBuffer();
        url.append(ccServerUrl);
        url.append("toolReceiving.asmx/toolReceivingHandle").toString();
        StringBuffer postParam = new StringBuffer();
        postParam.append("enginner_id=").append(params.get("enginnerId"));
        postParam.append("&tool_info=").append(params.get("toolInfo"));

        return pokeRequestServe.transPost(url.toString(), "工具收货", "toolReceiving", postParam.toString());
    }

    @Override
    public ResponseWithData<?> toolReturnLib(String params) throws Exception{
        String ccServerUrl = global.getCcServerUrl();
        StringBuffer url = new StringBuffer();
        url.append(ccServerUrl);
        url.append("toolReturnLib.asmx/toolReturnLibHandle").toString();

        JSONObject obj = JSON.parseObject(params);
        String mainStr = obj.get("applyMain").toString();
        String arrStr = obj.get("applyTools").toString();
        StringBuffer postParam = new StringBuffer("");
        if (org.apache.commons.lang3.StringUtils.isNotBlank(mainStr) && org.apache.commons.lang3.StringUtils.isNotBlank(arrStr)) {
            // 参数解析
            JSONObject mainInfo = JSON.parseObject(mainStr);
            // 参数拼接
            postParam.append("enginner_id=").append(mainInfo.get("enginner_id") == null ? "" : mainInfo.get("enginner_id"));
            postParam.append("&reexpressnumber=").append(mainInfo.get("reexpressnumber") == null ? "" : mainInfo.get("reexpressnumber"));
            postParam.append("&reexpresscompany=").append(mainInfo.get("reexpresscompany") == null ? "" : mainInfo.get("reexpresscompany"));
            postParam.append("&tool_info=").append(arrStr);
        }
        return pokeRequestServe.transPost(url.toString(), "工具还货", "toolReturnLib", postParam.toString());
    }

    @Override
    public ResponseWithData<?> applyBorrowTool(String params) throws Exception{
        String ccServerUrl = global.getCcServerUrl();
        StringBuffer url = new StringBuffer();
        url.append(ccServerUrl);
        url.append("applyBorrowTool.asmx/applyBorrowToolHandle").toString();

        JSONObject obj = JSON.parseObject(params);
        String mainStr = obj.get("applyMain").toString();
        String arrStr = obj.get("applyTools").toString();
        StringBuffer postParam = new StringBuffer("");
        if (org.apache.commons.lang3.StringUtils.isNotBlank(mainStr) && org.apache.commons.lang3.StringUtils.isNotBlank(arrStr)) {
            // 参数解析
            ApplyToolInfo mainInfo = JSON.parseObject(mainStr, ApplyToolInfo.class);
//            List<ToolInfo> toolList = JSON.parseArray(arrStr, ToolInfo.class);
            String mainInfoStr = pokeRequestServe.spellPrams(mainInfo);
            // 参数拼接
            postParam.append(mainInfoStr);
            postParam.append("&tool_info=").append(arrStr);
        }
        return pokeRequestServe.transPost(url.toString(), "工具借用申请", "applyBorrowTool", postParam.toString());
    }
}
